`
https://leetcode.cn/problems/unique-paths-ii/
`

/**
 * @param {number[][]} obstacleGrid
 * @return {number}
 */
var uniquePathsWithObstacles = function (obstacleGrid) {
  const m = obstacleGrid.length, n = obstacleGrid[0].length
  // dp[i][j] 表示从左上角 dp[0][0] 走到 dp[i][j] 的不同路径数量
  const dp = Array.from({ length: m }, () => new Array(n).fill(0))

  // 第一行和第一列肯定都是 1，如果有阻挡，那么阻挡及其后面的格子便都是 0
  // 处理第一行行
  for (let i = 0; i < n; i++) {
    if (obstacleGrid[0][i] === 1) break
    dp[0][i] = 1
  }
  // 处理第一列
  for (let i = 0; i < m; i++) {
    if (obstacleGrid[i][0] === 1) break
    dp[i][0] = 1
  }

  // dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
  for (let i = 1; i < m; i++) {
    for (let j = 1; j < n; j++) {
      if (obstacleGrid[i][j] === 1) continue
      dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
    }
  }

  return dp[m - 1][n - 1]
};